package org.example.likou150.hash;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


// https://leetcode.cn/problems/contains-duplicate-ii/description/?envType=study-plan-v2&envId=top-interview-150
public class ContainsNearbyDuplicate {
//    输入：nums = [1,2,3,1], k = 3
//    输出：true
    public static void main(String[] args) {

    }
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map=new HashMap<>();
        for (int i=0; i< nums.length; i++) {
            if(map.containsKey(nums[i]) && Math.abs(map.get(nums[i])-i) <= k){
                return true;
            } else{
                map.put(nums[i], i);
            }
        }
        return false;
    }

}
